﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Net;
using Comm.DBHelper;
using System.Reflection;
using Comm.PublicClass;
using System.Windows.Forms;
using Comm.MultiLan;

namespace WsProtocol.IEC62056MDL
{
    [Serializable]
    public class HHUFtp
    {
        private static string ptltable = "T_HHUFtp";

        public string IP { get; set; }  //IP
        public int Port { get; set; }  //端口
        public string UserName { get; set; } //FTP登录用户名
        public string Password { get; set; }   //FTP登录密码


        public HHUFtp()
        {
            try
            {
                IPAddress[] ips = Dns.GetHostAddresses("");
                IP = ips[ips.Length - 1].ToString();
            }
            catch
            { }
            Port = 21;
            UserName = "admin";
            Password = "admin";
        }

        public static void StartTrans()
        {
            PtlConsts.easyDb.BeginTrans();
        }

        public static void RollbackTrans()
        {
            PtlConsts.easyDb.RollbackTrans();
        }

        public static void CommitTrans()
        {
            PtlConsts.easyDb.CommitTrans();
        }


        private static HHUFtp TableToList(DataTable dt)
        {
            HHUFtp m = new HHUFtp();
            if (dt.Rows.Count > 0)
                m = GetModelByDataRow(dt.Rows[0]);

            return m;
        }


        /// <summary>
        /// 数据行转对象
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        public static HHUFtp GetModelByDataRow(DataRow dr)
        {
            HHUFtp m = new HHUFtp();
            m.IP = dr["IP"].ToString();
            m.Port = MyConvert.getInt32(dr["Port"].ToString());
            m.UserName = dr["UserName"].ToString();
            m.Password = dr["Password"].ToString();
            
            return m;
        }
       

        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <returns></returns>
        public static HHUFtp GetHHUFtp()
        {            
            PtlConsts.easyDb.Clear();
            PtlConsts.easyDb.TableName = ptltable;

            return TableToList(PtlConsts.easyDb.Select());
        }



        /// <summary>
        /// 保存用户信息到数据库 
        /// </summary>
        /// <param name="errinfo"></param>
        /// <returns></returns>
        public int SaveHHUFtp(ref string errinfo)
        {
            int rs = 0;
            PtlConsts.easyDb.Clear();
            PtlConsts.easyDb.TableName = ptltable;
            PtlConsts.easyDb.Delete(ref errinfo);


            PtlConsts.easyDb.Clear();
            PtlConsts.easyDb.TableName = ptltable;
            PtlConsts.easyDb.SetFieldValue("IP", IP, EasyDataType.String);
            PtlConsts.easyDb.SetFieldValue("Port", Port.ToString(), EasyDataType.Number);
            PtlConsts.easyDb.SetFieldValue("UserName", UserName, EasyDataType.String);
            PtlConsts.easyDb.SetFieldValue("Password", Password, EasyDataType.String);

            rs = PtlConsts.easyDb.Insert(ref errinfo);
            if (rs <= 0)
                rs = -1;
            
            return rs;
        }


    }
}
